package cn.suxiaoxiang.suwebverify.events;

import cn.suxiaoxiang.suwebverify.SuWebVerify;
import cn.suxiaoxiang.suwebverify.config.MessageConfig;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.util.Vector;

import org.bukkit.configuration.file.FileConfiguration;

import static cn.suxiaoxiang.suwebverify.SuWebVerify.main;
import static cn.suxiaoxiang.suwebverify.events.PlayerJoin.PLAYER_SESSIONS;
import org.bukkit.event.EventPriority;

public class onPlayerAction implements Listener {

    private MessageConfig messageConfig;
    // 在构造器中注入 messageConfig
    public onPlayerAction(SuWebVerify plugin) {
        this.messageConfig = new MessageConfig(plugin);
        this.messageConfig.loadPlayerCommandConfig();  // 加载配置文件
    }
    //获取插件前缀
    String prefix = main.getConfig().getString("prefix");
    // 禁止玩家移动
    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        Vector from = event.getFrom().toVector();
        Vector to = event.getTo().toVector();

        boolean isRegistered = PLAYER_SESSIONS.getOrDefault(player.getName(), false);
        boolean disableMove = main.getConfig().getBoolean("behavior.disableMove");
        String playerMove = messageConfig.getPlayerCommandConfig().getString("PlayerMove");
        if (!isRegistered && disableMove) {
            if (!from.equals(to)) {
                event.setCancelled(true);
                player.sendMessage(prefix+playerMove);
            }
        }
    }
    // 禁止玩家切换手持物品
    @EventHandler(priority = EventPriority.HIGH)
    public void onItemHeld(PlayerItemHeldEvent event) {
        Player player = event.getPlayer();

        boolean isRegistered = PLAYER_SESSIONS.getOrDefault(player.getName(), false);
        boolean disableItemSwitch = main.getConfig().getBoolean("behavior.disableItemSwitch");
        String playerSwitchItem = messageConfig.getPlayerCommandConfig().getString("PlayerSwitchItem");
        if (!isRegistered && disableItemSwitch) {
            event.setCancelled(true);
            player.sendMessage(prefix+playerSwitchItem);
        }
    }

    // 禁止玩家打开背包
    @EventHandler(priority = EventPriority.HIGH)
    public void onInventoryOpen(InventoryOpenEvent event) {
        Player player = (Player) event.getPlayer();

        boolean isRegistered = PLAYER_SESSIONS.getOrDefault(player.getName(), false);
        boolean disableInventoryOpen = main.getConfig().getBoolean("behavior.disableInventoryOpen");
        String playerOpenInventory = messageConfig.getPlayerCommandConfig().getString("PlayerOpenInventory");
        if (!isRegistered && disableInventoryOpen) {
            event.setCancelled(true);
            player.sendMessage(prefix+playerOpenInventory);
        }
    }
    @EventHandler(priority = EventPriority.HIGH)
    public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();

        // 获取玩家注册状态
        boolean isRegistered2 = PLAYER_SESSIONS.getOrDefault(player.getName(), false);

        // 获取配置文件中的设置
        boolean disableInventoryOpen = main.getConfig().getBoolean("behavior.disableInventoryOpen");

        // 获取玩家打开背包的消息
        String playerOpenInventory = messageConfig.getPlayerCommandConfig().getString("PlayerOpenInventory");

        // 判断玩家是否注册且是否开启禁用背包的设置
        if (!isRegistered2 && disableInventoryOpen) {
            // 取消点击背包事件
            event.setCancelled(true);
            // 发送禁止打开背包的消息
            player.sendMessage(prefix+playerOpenInventory);
        }
    }
    // 禁止玩家使用物品
    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();

        boolean isRegistered = PLAYER_SESSIONS.getOrDefault(player.getName(), false);
        boolean disableItemUse = main.getConfig().getBoolean("behavior.disableItemUse");
        String playerUseItem = messageConfig.getPlayerCommandConfig().getString("PlayerUseItem");
        if (!isRegistered &&disableItemUse) {
            event.setCancelled(true);
            player.sendMessage(prefix+playerUseItem);
        }
    }

    // 禁止玩家发送聊天消息
    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerChat(AsyncPlayerChatEvent event) {
        Player player = event.getPlayer();

        boolean isRegistered = PLAYER_SESSIONS.getOrDefault(player.getName(), false);
        boolean disableChat = main.getConfig().getBoolean("behavior.disableChat");
        String playerSendMessage = messageConfig.getPlayerCommandConfig().getString("PlayerSendMessage");
        if (!isRegistered && disableChat) {
            event.setCancelled(true);
            player.sendMessage(prefix+playerSendMessage);
        }
    }

    // 禁止玩家使用指令
    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
        Player player = event.getPlayer();

        boolean isRegistered = PLAYER_SESSIONS.getOrDefault(player.getName(), false);
        boolean disableCommands = main.getConfig().getBoolean("behavior.disableCommands");
        String playerUseCommand = messageConfig.getPlayerCommandConfig().getString("PlayerUseCommand");
        if (!isRegistered && disableCommands) {
            event.setCancelled(true);
            player.sendMessage(prefix+playerUseCommand);
        }
    }
    // 禁止玩家丢弃物品
    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerDropItem(PlayerDropItemEvent event) {
        Player player = event.getPlayer();

        boolean isRegistered = PLAYER_SESSIONS.getOrDefault(player.getName(), false);
        boolean disableDropItem = main.getConfig().getBoolean("behavior.disableDropItem");
        String playerDropItem = messageConfig.getPlayerCommandConfig().getString("PlayerDropItem");
        if (!isRegistered && disableDropItem) {
            event.setCancelled(true);
            player.sendMessage(prefix+playerDropItem);
        }
    }
}
